package cn.icanci.ladybird.service.admin.impl;

import cn.icanci.ladybird.common.util.CommonUtils;
import cn.icanci.ladybird.common.util.JWTUtils;
import cn.icanci.ladybird.common.util.RedisUtils;
import cn.icanci.ladybird.pojo.bo.AdminLoginBO;
import cn.icanci.ladybird.pojo.entity.Admin;
import cn.icanci.ladybird.service.AdminService;
import cn.icanci.ladybird.service.admin.AdminLoginService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author: icanci
 * @date: Created in 2020/10/8 16:12
 * @classAction: 管理员登录的实现类
 */
@Service
public class AdminLoginServiceImpl implements AdminLoginService {
    @Autowired
    private AdminService adminService;
    @Autowired
    private RedisUtils redisUtils;

    @Override
    public String queryByAdminLoginBO(HttpServletRequest request, HttpServletResponse response, AdminLoginBO adminLoginBO) throws IOException {
        Admin admin = adminService.queryByUsernameAndPassword(adminLoginBO.getUsername(), CommonUtils.MD5(adminLoginBO.getPassword()));
        if (null == admin) {
            return null;
        } else {
            String token = JWTUtils.generateAdminJWTWebToken(admin);
            // TODO 根据用户id向  Redis 放置
            redisUtils.set(String.valueOf(admin.getAdminId()), token, 30, TimeUnit.MINUTES);
            // 设置头像地址
            initPage(request, response, token);
            return token;
        }
    }

    /**
     * 界面数据初始化
     *
     * @param request  请求
     * @param response 响应
     * @param token    需要处理的token
     * @throws IOException
     */
    private void initPage(HttpServletRequest request, HttpServletResponse response, String token) throws IOException {
        Claims claims = JWTUtils.checkJWT(token);
        String adminHeadImg = (String) claims.get("admin_head_img");
        Integer adminId = (Integer) claims.get("admin_id");
        Integer adminEnabled = (Integer) claims.get("admin_enabled");
        String adminName = (String) claims.get("admin_name");
        Integer admin_authority = (Integer) claims.get("admin_authority");
        // 如果不可用，就不能登录
        if (adminEnabled == 0) {
            response.sendRedirect("/api/v1/pri/admin/403");
        }
        request.setAttribute("admin_head_img", adminHeadImg);
        request.setAttribute("admin_id", adminId);
        request.setAttribute("admin_enabled", adminEnabled);
        request.setAttribute("admin_name", adminName);
        request.setAttribute("admin_name", adminName);
        String authority = "管理员";
        switch (admin_authority) {
            case 0:
                authority = "顶级管理员";
                break;
            case 1:
                authority = "一级管理员";
                break;
            case 2:
                authority = "二级管理员";
                break;
            case 3:
                authority = "三级管理员";
                break;
            default:
                break;
        }
        request.setAttribute("admin_authority", authority);
    }
}
